#include "system.h"
#include "list.h"
#include "file.h"
#include "comment.h"
 
char userName[20];
char * showSelect(char * fileName){
    FILE * pf = fopen(fileName, "r");
    char *p = readString(pf,300);
    printf("%s\n",p);
    fclose(pf);
    char * pSelect= malloc(7);
    printf("你选择的操作为：");
    scanf("%s",pSelect);
    return pSelect;
}

int userLogin(){
        USER user;
        printf("输入用户名：");
        scanf("%s",user.name);
        printf("输入密码：");
        scanf("%s",user.password);
        struct node * pHead=loginLoadUsers();
        struct node * pTemp = pHead->pNext;
        while (pTemp!=pHead)
        {
        if(strcmp(user.name,((USER*)pTemp->pData)->name)==0&&strcmp(user.password,((USER*)pTemp->pData)->password)==0){
            system("clear");
            printf("登录成功！\n");
            strcpy(userName,user.name);
            printf("%s用户登录中！\n",user.name);
            return 1;
        }else{
            pTemp = pTemp->pNext;
        }
        }
         printf("用户名或密码错误！\n");
         return 0 ;
}

int adminLogin(){
        USER user;
        printf("输入用户名：");
        scanf("%s",user.name);
        printf("输入密码：");
        scanf("%s",user.password);
        if(strcmp(user.name,"admin")==0&&strcmp(user.password,"admin")==0){  
            strcpy(userName,user.name);
            system("clear");
            printf("登录成功！\n");
            printf("此刻处于%s用户登录中\n",user.name);
            return 1;
        }else{
            printf("用户名或密码错误！\n");
            return 0 ;
        }
}

void userRegist(){
    USER *user= createUser();
    FILE *pf = fopen("UI/users","ab+");
    write(pf,user,sizeof(USER));
    fputc('\n',pf);            //换行
    fclose(pf);
    printf("注册成功！\n");
    strcat(userName,user->name);
    char p[20] = "user_data/";
    strcat(p,user->name);
    char * pa = p;
    FILE *pf1 = fopen(pa,"wb+");    //创建与用户名相关缓存文件
    UT * ut = malloc(sizeof(UT));
    ut->cash = 200;
    ut->ticket=NULL;
    write(pf1,ut,sizeof(UT));
    free(ut);
    fputc('\n',pf1);
    fclose(pf1);
}

struct node * loginLoadUsers(){
  struct node *pHead=createLists(pHead);
     FILE * pf = openFile("UI/users","rb+");
     //先分配一个空间
     USER * p = malloc(sizeof(USER));
     //读取一个用户的字节数
     int ret = read(pf,p,sizeof(USER));
     //如果返回值不为0说明读取成功了，否则说明文件已经到了末尾
     while(ret != 0)
     {
          fgetc(pf);
          //把读取的数据地址加入到链表中
          add(pHead,p);
          //printf("%d,%s,%s\n",p->id,p->name,p->password);
          //重新分配一个空间
          p = malloc(sizeof(USER));
          //继续读取下面的数据
          ret = read(pf,p,sizeof(USER));
     }
     if(ret == 0)
     {
          free(p);
     }
     closeFile(pf);
     return pHead;
}

void showUser(){
    FILE * pf = fopen("UI/users","rb+");
    USER * p = malloc(sizeof(USER));
    int ret = read(pf,p,sizeof(USER));
    while(ret!= 0)
    {
        fgetc(pf);
        printf("用户ID：%d 用户名：%s 密码：%s\n",p->id,p->name,p->password);
        p = malloc(sizeof(USER));
        ret = read(pf,p,sizeof(USER));
    }
    if(ret == 0)
    {
        free(p);
    }
    fclose(pf);
}

void change(){

    struct node * pHead=loginLoadUsers();
        struct node * pTemp = pHead->pNext;
        while (pTemp!=pHead)
        {
        if(strcmp(userName,((USER*)pTemp->pData)->name)==0){
            system("clear");
            printf("输入修改后的密码：");
        scanf("%s",((USER*)pTemp->pData)->password);
        printf("修改成功！按任意键继续\n");
               getchar();
               getchar();
        saveListToFile("UI/users",pHead,sizeof(USER));
            return ;
        }else{
            pTemp = pTemp->pNext;
        }
        }
         return  ;
}